<template>
  <!-- 借款添加 -->
  <div class="contractInfo box">
    <el-divider content-position="left">{{
      $route.query.mode == "add" ? "添加借款" : "编辑借款"
    }}</el-divider>
    <div class="formBox">
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <!-- <el-form-item label="借款编号" prop="num" required style="width: 100%">
          <p>{{ form.num }}</p>
        </el-form-item> -->
        <el-form-item label="借款名称" prop="loanName">
          <el-input
            v-model="form.loanName"
            clearable
            placeholder="请输入回款名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="借款时间" prop="loanTime">
          <el-date-picker
            v-model="form.loanTime"
            type="date"
            placeholder="请选择回款日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="借款金额" prop="loanMoney" class="priceItem">
          <el-input
            v-model="form.loanMoney"
            clearable
            placeholder="请输入借款金额"
            oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
            @blur="form.loanMoney = $event.target.value"
          >
            <template #append>元</template></el-input
          >
        </el-form-item>

        <el-form-item label="类型" prop="loanType">
          <el-select
            v-model="form.loanType"
            placeholder="请选择"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in loanType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="operatorName">
          <el-input
            v-model="form.operatorName"
            clearable
            placeholder="请选择经办人"
            @click="selectOpertaor"
          >
            <!-- <template #append>
              <el-button slot="append">请选择</el-button>
            </template> -->
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="width: 100%">
          <el-input
            v-model="form.remark"
            clearable
            type="textarea"
            rows="3"
            placeholder="请输入备注内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件">
          <sc-upload-file
            v-model="form.otherFileList"
            :limit="10"
            :multiple="false"
            tip="最多上传10个文件,单个文件不要超过20M"
          >
            <el-button type="primary" plain icon="el-icon-upload"
              >上传附件</el-button
            >
          </sc-upload-file>
        </el-form-item>
      </el-form>

      <!-- 审核流程项 -->
      <check-form-item
        ref="checkFormItem"
        @setCheckConfig="setCheckConfig"
        :field="this.form"
        :checkType="checkType"
        v-if="$route.query.mode == 'add'"
      ></check-form-item>
      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </div>

    <!-- 选择经办人 -->
    <select-user ref="selectUser" @setUser="setOpertaor"></select-user>
  </div>
</template>
<script>
import checkFormItem from "@/components/checkProcess/checkFormItem.vue"
import { queryTypeByDic } from "@/utils/getData"
import selectUser from "@/components/selectUser"
export default {
  name: "customerAdd",
  data () {
    return {
      checkType: 9, //工作流的类型
      isSaveing: false,
      selectContractLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        adminId: "", //跟进人id --登录时id
        departmentId: "",
        // contractId: '',
        operatorId: "", //经办人id
        operatorName: "", //经办人
        loanType: "", //类型
        loanName: "", //借款名称
        loanMoney: "", //借款金额
        loanTime: this.$TOOL.dateFormat(new Date(), "yyyy-MM-dd"), //借款时间
        remark: "", //备注
        exempt: "", //是否免审
        otherFileList: [], //附件
        workflowJsonList: [],
      },
      loanType: [], //借款类型
      //验证规则
      rules: {
        loanName: [{ required: true, message: "请输入借款名称" }],
        loanMoney: [{ required: true, message: "请输入借款金额" }],
        loanTime: [
          {
            type: "date",
            required: true,
            message: "请选择借款日期",
            trigger: "change",
          },
        ],
        loanType: [
          {
            required: true,
            message: "请选择借款类型",
            trigger: "change",
          },
        ],
        operatorName: [
          {
            required: true,
            message: "请选择经办人",
            trigger: "change",
          },
        ],
      },
    }
  },
  components: {
    checkFormItem,
    selectUser,
  },
  mounted () {
    this.getType()
    if (this.$route.query.mode == "edit") {
      this.getLoanById()
    }
  },
  methods: {
    // 获取分类类型
    async getType () {
      // this.loanType = await queryTypeByDic('expenseCategory')
      let { data } =
        await this.$API.finance.expenseType.getExpenseTypeList.get({
          page: 1,
          limit: 1000,
          isDisable: 0, //0启用1禁用
        })
      this.loanType = data
    },

    // 审批流配置
    setCheckConfig (list, exempt) {
      this.form.workflowJsonList = list
      this.form.exempt = exempt
    },

    // 选择经办人
    selectOpertaor () {
      this.$refs.selectUser.open(1)
    },

    setOpertaor (data) {
      this.form.operatorId = data.id
      this.form.operatorName = data.name
    },

    // 获取借款信息
    async getLoanById () {
      let res =
        await this.$API.finance.loanManagement.getLoanDetailById.post({
          id: this.$route.query.id,
        })
      this.form = res.data
      // this.form.otherFileList = res.data.otherFileList
      this.form.otherFileList.forEach((item) => {
        item.name = item.fileName
      })
    },

    async save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let userInfo = this.$TOOL.data.get("USER_INFO")
          this.form.departmentId = userInfo.departmentId
          this.form.adminId = userInfo.userId

          let fileType = ""
          this.form.otherFileList.map((item) => {
            if (item.url) {
              let arr = item.url.split(".")
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = "jpg/png/jpeg/svg"
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })

          let res = null
          let mode = this.$route.query.mode
          if (mode == "add") {
            res =
              await this.$API.finance.loanManagement.addLoan.post(
                this.form
              )
          } else if (mode == "edit") {
            delete this.form.status
            delete this.form.createTime
            delete this.form.updateTime
            res =
              await this.$API.finance.loanManagement.updateLoan.post(
                this.form
              )
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode)
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: "/finance/loanManagement",
            })
            this.$message.success("操作成功")
          }
        }
      })
    },

    // 取消客户添加
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: "/finance/loanManagement",
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;
  .el-form {
    width: 100%;
  }
}
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>
